package com.htfyun.fragment.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by sst on 2017/12/29.
 */

public abstract class BaseWithPageNumberAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {

    private LayoutInflater mInflater;
    private Context mContext;
    
    private final List<T> items =
            Collections.synchronizedList(new ArrayList<T>());

    private OnAdapterBindViewHolderListener onAdapterBindViewHolderListener;
    public interface OnAdapterBindViewHolderListener {
        public void onBindStart();
        public void onBindEnd();
    }
    
    public BaseWithPageNumberAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        mContext = context;
    }
    
    public void setItemsAndNotify(List<T> items) {
        this.items.clear();
        this.items.addAll(items);
        notifyDataSetChanged();
    }

    public OnAdapterBindViewHolderListener getOnAdapterBindViewHolderListener() {
        return onAdapterBindViewHolderListener;
    }

    public void setOnAdapterBindViewHolderListener(OnAdapterBindViewHolderListener onAdapterBindViewHolderListener) {
        this.onAdapterBindViewHolderListener = onAdapterBindViewHolderListener;
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(getLayoutId(), parent, false);
        return getViewHolder(view);
        
    }

    @Override
    public void onBindViewHolder(VH holder, int pos) {
        final int position = pos;
        if (getItemCount() <= position) {
            return;
        }

        if (onAdapterBindViewHolderListener != null) {
            if (pos == 0) {
                onAdapterBindViewHolderListener.onBindStart();
            } 
            if (pos == getItemCount() - 1) {
                onAdapterBindViewHolderListener.onBindEnd();
            }
        }
      
        
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position < getItemCount()) {
                    T t = getItems().get(position);
                    onItemClick(position, t);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {


                if (position < getItemCount()) {
                    T t = getItems().get(position);

                    return onItemLongClick(position, t);
                }
                return false;
            }
        });
        
        
    }

    @Override
    public int getItemCount() {
        return this.items.size();
    }

    public List<T> getItems() {
        return items;
    }

    public Context getContext() {
        return mContext;
    }
    
    public abstract VH getViewHolder(View view);
    public abstract int getLayoutId();
    public abstract void onItemClick(int position, T t);

    public boolean onItemLongClick(int position, T t) {
        return false;
    }
    
}
